package com.weishu.installment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishu.installment.domain.SfContractEntity;
import com.weishu.installment.domain.TJrProduct;
import com.weishu.installment.domain.TJrSfLoan;
import com.weishu.installment.domain.shoufang.SearchSfLoanDetailMiniResponseDto;
import com.weishu.installment.domain.shoufang.SearchSfLoanDetailRequestDto;
import com.weishu.installment.domain.shoufang.SearchSfLoanDetailsResponseDto;
import com.weishu.installment.mapper.SfContractEntityMapper;
import com.weishu.installment.mapper.TJrProductMapper;
import com.weishu.installment.service.TJrProductService;
import com.weishu.installment.service.TJrSfLoanService;
import com.weishu.installment.mapper.TJrSfLoanMapper;
import com.weishu.installment.utils.MyJinRongUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【t_jr_sf_loan(收房贷申请信息)】的数据库操作Service实现
* @createDate 2023-05-06 08:30:59
*/
@Service
public class TJrSfLoanServiceImpl extends ServiceImpl<TJrSfLoanMapper, TJrSfLoan>
    implements TJrSfLoanService{
    @Autowired
    TJrProductMapper jrProductService;
    @Autowired
    SfContractEntityMapper sfContractEntityMapper;

    public SearchSfLoanDetailsResponseDto getSfLoanDetail(SearchSfLoanDetailRequestDto sfdto) {
        System.out.println("getSfLoanDetail收房贷计算分期信息处理Start...");
        System.out.println("----收房贷计算分期入参：{}"+JSON.toJSONString(sfdto));
        SearchSfLoanDetailsResponseDto dto = new SearchSfLoanDetailsResponseDto();
        List<SearchSfLoanDetailMiniResponseDto> spList = new ArrayList<SearchSfLoanDetailMiniResponseDto>();
        TJrProduct product = jrProductService.selectById(sfdto.getProductId());
        //授信率
        Double creditLine = MyJinRongUtils.div(product.getCreditLine(), 100, 2);
        //一次性服务费
        double serviceFee = MyJinRongUtils.div(Double.parseDouble(sfdto.getServiceFee()), 100, 4);
        //利率
        double rate = MyJinRongUtils.div(Double.parseDouble(sfdto.getRate()), 100, 4);// 利率

        //授信额合计
        double sxBalance = MyJinRongUtils.mul(sfdto.getBalance(), creditLine);

        // 合同期数
        Long sfperiod = sfdto.getPeriods();
        //转换为int类型
        int sfperiods=sfperiod.intValue();

        //服务费合计=   (借款金额*一次性服务费 /12 * 期数 * 授信额)
        double ser_Charge=MyJinRongUtils.mul(sfdto.getBalance(), serviceFee);
        double ser_Charge_ = MyJinRongUtils.div(ser_Charge, 12, 2);
        double _ser_Charge=MyJinRongUtils.mul(ser_Charge_, sfperiods);
        double serCharge=MyJinRongUtils.mul(_ser_Charge, creditLine);

        // 计算 月租金
        double month_RateAmount=MyJinRongUtils.div(sfdto.getBalance(),sfperiods,2);
        // 借款总额(应付租约合计 )   = 月租金* 期数 * 授信
        double monthAmount = MyJinRongUtils.mul(month_RateAmount, sfperiods);
        double payAbleCount_ = MyJinRongUtils.mul(monthAmount,creditLine);

        // 计算收房贷利息合计= 借款金额* 利率/12  * 授信额 * 期数
        //借款金额* 利率
        double rates= MyJinRongUtils.mul(payAbleCount_, rate);
        // 利率/12
        double  monthRateAmounts_=MyJinRongUtils.div(rates,12,2);
        double  monthRateAmounts=MyJinRongUtils.mul(monthRateAmounts_,sfperiods);

        //利息合计 InterestCount
        double InterestCount=0;
        //还款总额合计      (账期还款金额的合计)
        double rePaymentCount=0;
        //应付租约合计      (总金额)
        double payAbleCount=payAbleCount_;
        // 根据一段时间区间，按月份拆分成多个时间段
        List<MyJinRongUtils.KeyValueForDate> splitMonth = MyJinRongUtils.getSplitMonthForDate(sfdto.getStartDate(), sfdto.getEndDate());
        //收房合同
        SfContractEntity sfentity = sfContractEntityMapper.selectById(sfdto.getContractId());
        int no=1;

        SearchSfLoanDetailMiniResponseDto sfdmini=null;
        // 账期利息 （每月利息）= 利息总和/ 期数
        double  rateAmount=MyJinRongUtils.div(monthRateAmounts,sfperiods,2);
        //计算月租金
        // 账期还款金额（每月账期还款金）= 还款总额合计 / 期数
        double repaymentAmount_=MyJinRongUtils.div(payAbleCount,sfperiods,2);
        double repaymentAmount = MyJinRongUtils.add(repaymentAmount_, rateAmount);
        //利息合计
        InterestCount=MyJinRongUtils.add(InterestCount, monthRateAmounts);
        // 还款总额合计  = 账期还款金额（每月账期还款金）* 期数
        rePaymentCount=MyJinRongUtils.mul(repaymentAmount,sfperiods);
        // 循环计算分期详情列表
        for (MyJinRongUtils.KeyValueForDate v : splitMonth) {
            sfdmini=new SearchSfLoanDetailMiniResponseDto();
            sfdmini.setNo(no);
            // 房产编号
            sfdmini.setRoomCode(sfentity.getHouseId());
            // 房产信息 = 小区名+房产信息
            sfdmini.setRealEstateInfo(sfentity.getPremiseName()+sfentity.getHouseName());
            // 利息
            sfdmini.setMonthRateAmount(rateAmount);
            // 月租金
            sfdmini.setMonthRepaymentAmount(repaymentAmount);
            // 合同开始日
            sfdmini.setStarLoanday(v.getStartDate());
            // 合同结束日
            sfdmini.setEndLoanday(v.getEndDate());
            spList.add(sfdmini);
            no++;
        }
        dto.setPayAbleCount(payAbleCount + "");// 应付租约合计
        dto.setRePaymentCount(rePaymentCount + "");// 还款总额合计
        dto.setInterestCount(InterestCount + "");// 收房贷利息合计
        dto.setSerCharge(serCharge + "");// 一次性服务费
        dto.setProductId(product.getId());//产品id
        dto.setSxAbleCount(sxBalance + "");
        dto.setSfLoan(spList);
        dto.setId(sfdto.getContractId());//合同id

        dto.setServiceFee(sfdto.getServiceFee());//一次性服务费率
        dto.setRate(sfdto.getRate());//利率

        System.out.println("getSfLoanDetail收房贷计算分期信息处理end...");
        return dto;

    }

}




